local skel = fk.CreateSkill {
  name = "emo__buxiuzhe",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__buxiuzhe"] = "不朽之者",
  [":emo__buxiuzhe"] = "出牌阶段各限一次，你可以：①弃置当前体力值张牌；②将手牌摸至体力上限；③背水：收回“枯败的王”已失去的一项。",
  -- 背水也在各限一次中，即可以发动三次

  ["#emo__buxiuzhe"] = "不朽之者：你可选一项执行",
  ["emo__buxiuzhe_draw"] = "将手牌摸至体力上限",
  ["emo__buxiuzhe_throw"] = "弃置当前体力值张牌",
  ["emo__buxiuzhe_both"] = "背水:先弃，后摸",
  ["#emo__buxiuzhe-choice"] = "不朽之者：选择收回“枯败的王”已失去的一项",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#emo__buxiuzhe",
  min_card_num = 1,
  target_num = 0,
  interaction = function(self, player)
    local all_choices = { "emo__buxiuzhe_draw", "emo__buxiuzhe_throw", "emo__buxiuzhe_both" }
    local choices = table.filter(all_choices, function(choice)
      return player:getMark(choice .. "-phase") == 0
    end)
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < player.hp
    and self.interaction.data ~= "emo__buxiuzhe_draw"
    and not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  feasible = function (self, player, selected, selected_cards)
    if self.interaction.data == "emo__buxiuzhe_draw" then
      return player:getHandcardNum() < player.maxHp
    else
      return #selected_cards == player.hp
    end
  end,
  can_use = function(self, player)
    local all_choices = { "emo__buxiuzhe_draw", "emo__buxiuzhe_throw", "emo__buxiuzhe_both" }
    return table.find(all_choices, function(choice)
      return player:getMark(choice .. "-phase") == 0
    end) ~= nil
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local choice = effect.interaction_data
    if choice == nil then return end
    room:setPlayerMark(player, choice .. "-phase", 1)
    if #effect.cards > 0 then
      room:throwCard(effect.cards, skel.name, player, player)
      if player.dead then return end
    end
    if choice ~= "emo__buxiuzhe_throw" then
      local num = player.maxHp - player:getHandcardNum()
      if num > 0 then
        player:drawCards(num, skel.name)
      end
    end
    if not player.dead and choice == "emo__buxiuzhe_both" then
      local choices = table.filter({"emo__kubaiwang_hp", "emo__kubaiwang_maxhp", "emo__kubaiwang_card"}, function (c)
        return player:getMark(c) == 0
      end)
      if #choices == 0 then return end
      local get = room:askToChoice(player, { choices = choices, skill_name = skel.name, prompt = "#emo__buxiuzhe-choice" })
      room:setPlayerMark(player, get, 1)
      if get == "emo__kubaiwang_hp" then
        if player.hp < player.maxHp then
          room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
        else -- 搞笑
          room:setPlayerProperty(player, "hp", player.hp + 1)
        end
      elseif get == "emo__kubaiwang_maxhp" then
        room:changeMaxHp(player, 1)
      elseif get == "emo__kubaiwang_card" then
        local cards = table.filter(player:getTableMark("emo__kubaiwang_cardData"), function (id)
          return room:getCardArea(id) ~= Card.PlayerSpecial and room:getCardArea(id) ~= Card.Void
        end)
        if #cards > 0 then
          room:obtainCard(player, cards, true, fk.ReasonPrey, player, skel.name, {"@@emo__kubaiwang-inhand", player.id})
        end
      end
    end
  end,
})

return skel
